package jinyilw.swing.rlaf.ui;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.rlaf.theme.RThemeColor;
import jinyilw.swing.rlaf.utils.RBorderFactory;

import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import javax.swing.plaf.basic.BasicHTML;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Stroke;

/**
 * @author Jinyi
 */
public class RButtonUI extends BasicButtonUI
{
	private static final Border rBorder = RBorderFactory.createButtonBorder();
	private static final Border iBorder = RBorderFactory
			.createIconButtonBorder();
	protected static final AlphaComposite alphaComposite = ComponentTools.alphaComposite;
	protected static final Color lightColor = new ColorUIResource(
			new Color(220, 220, 220, 200));
	protected static Color darkBorderColor = new Color(0, 0, 0, 130);
	protected static Color lightBorderColor = new Color(150, 150, 150, 100);
	protected static Stroke stroke = new BasicStroke(1.2f);
	protected float defaultAlpha = 0.3f, hoverAlpha = 0.45f;
	protected Color graColor1 = lightColor;
	protected Color graColor2 = Color.white;
	protected int round = 0;
	protected RenderingConfig renderingConfig = new RenderingConfig();

	protected AbstractButton b;

	public static ComponentUI createUI(JComponent c)
	{
		return new RButtonUI();
	}

	@Override
	public void installUI(JComponent c)
	{
		super.installUI(c);
		b = (AbstractButton) c;
		b.setFont(FontTools.defaultFont);
		b.setBorderPainted(false);
		b.setFocusPainted(false);
		b.setForeground(RThemeColor.colorSet.textColor);
		b.setBackground(lightColor);
	}

	public void setDefaultAlpha(float alpha)
	{
		defaultAlpha = alpha;
	}

	public void setHoverAlpha(float alpha)
	{
		hoverAlpha = alpha;
	}

	public void configAlpha(float dAlpha, float hAlpha)
	{
		defaultAlpha = dAlpha;
		hoverAlpha = hAlpha;
	}

	public void setGradientColor(Color color1, Color color2)
	{
		if (color1 == null || color2 == null)
			return;
		graColor1 = color1;
		graColor2 = color2;
	}

	public void setRound(int round)
	{
		this.round = round;
	}

	@Override
	protected void installDefaults(AbstractButton b)
	{
		String pp = getPropertyPrefix();
		defaultTextShiftOffset = UIManager.getInt(pp + "textShiftOffset");

		LookAndFeel.installProperty(b, "opaque", Boolean.FALSE);

		if (b.getMargin() == null || (b.getMargin() instanceof UIResource))
			b.setMargin(UIManager.getInsets(pp + "margin"));

		LookAndFeel.installProperty(b, "rolloverEnabled", Boolean.TRUE);
		LookAndFeel.installProperty(b, "iconTextGap", 4);
	}

	@Override
	public Dimension getPreferredSize(JComponent c)
	{
		if (b.getBorder() == null || b.getBorder() instanceof UIResource)
		{
			if (b.getIcon() != null
					&& (b.getText() == null || b.getText().isEmpty()))
				b.setBorder(iBorder);
			else
				b.setBorder(rBorder);
		}
		return super.getPreferredSize(c);
	}

	@Override
	public void update(Graphics g, JComponent c)
	{
		int h = b.getHeight(), w = b.getWidth();
		int arc = round > 0 ? round : (int) (h * 0.8);

		boolean isEnabled = b.isEnabled();
		boolean isRollover = b.getModel().isRollover();
		boolean isPressed = b.getModel().isPressed();
		float alpha = !isEnabled ? 0.2f : isRollover ? hoverAlpha : defaultAlpha;

		Graphics2D g2d = (Graphics2D) g;
		Composite composite = g2d.getComposite();
		renderingConfig.setAllAAPainting(g2d);

		GradientPaint borderPaint;
		//		if (isPressed)
		//		{
		//			borderPaint = new GradientPaint(1, 1, darkBorderColor, 1, h - 2,
		//			lightBorderColor);
		//			alpha = hoverAlpha - 0.08f;
		//		} else
		borderPaint = new GradientPaint(1, 1, lightBorderColor, 1, h - 1,
				darkBorderColor);

		g2d.setComposite(alphaComposite.derive(alpha));
		//		if (bColor instanceof UIResource)
		//			g2d.setColor(lightColor);
		//		else
		g2d.setColor(c.getBackground());
		//			g2d.setPaint(
		//					new GradientPaint(1, 1, graColor1, 0, h, graColor2, true));
		g2d.fillRoundRect(1, 1, w - 2, h - 2, arc, arc);

		g2d.setComposite(alphaComposite.derive(isEnabled ? 0.85f : 0.3f));

		if (isEnabled && isRollover)
			g2d.setColor(RThemeColor.themeColor);
		else
			g2d.setPaint(borderPaint);
		Stroke saveStroke = g2d.getStroke();
		g2d.setStroke(stroke);
		g2d.drawRoundRect(1, 1, w - 2, h - 2, arc, arc);
		g2d.setStroke(saveStroke);

		if (c.getClientProperty(BasicHTML.propertyKey) != null)
			RThemeColor.customForegroundSetting(c);
		g2d.setComposite(alphaComposite.derive(isEnabled ? 1f : 0.35f));
		if (isEnabled && isPressed)
			g2d.translate(0.8, 0.8);
		paint(g2d, c);
		if (isEnabled && isPressed)
			g2d.translate(-0.8, -0.8);
		renderingConfig.resetAllAAPainting(g2d);
		g2d.setComposite(composite);
	}

	@Override
	protected void paintText(Graphics g, AbstractButton b, Rectangle textRect,
			String text)
	{
		FontMetrics fm = g.getFontMetrics();
		int mnemonicIndex = b.getDisplayedMnemonicIndex();
		Color fColor = b.getForeground();
		if (fColor == null || fColor instanceof UIResource)
			g.setColor(RThemeColor.colorSet.textColor);
		else
			g.setColor(fColor);
		BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemonicIndex,
				textRect.x + getTextShiftOffset(),
				textRect.y + fm.getAscent() + getTextShiftOffset());
	}
}
